/**
 * 
 */
package fpt.pits.dao;

import java.sql.SQLException;

import fpt.pits.dto.ProjectInfo;
import fpt.pits.util.Connection;

/**
 * @author yoyosakura
 *
 */
public class ProjectInfoDAO {
	public ProjectInfo get(String projectName) {
		ProjectInfo projectInfo = null;
		java.sql.Connection cn = null;
		java.sql.PreparedStatement ps = null;
		java.sql.ResultSet rs = null;
		String query = null;
		try {
            query = "SELECT p.* " +
            		"FROM `project` p " +
            		"WHERE p.ProjectName = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setString(1, projectName);
            rs = ps.executeQuery();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                	projectInfo = new ProjectInfo();
                	projectInfo.setDepartID(rs.getInt("DepartID"));
                	projectInfo.setDescription(rs.getString("Description"));
                	projectInfo.setIsDeleted(rs.getByte("IsDeleted"));
                	projectInfo.setLastModified(rs.getString("LastModified"));
                	projectInfo.setModifiedID(rs.getString("ModifiedID"));
                	projectInfo.setPeriodFrom(rs.getString("PeriodFrom"));
                	projectInfo.setPeriodTo(rs.getString("PeriodTo"));
                	projectInfo.setProjectID(rs.getInt("ProjectID"));
                	projectInfo.setProjectName(rs.getString("ProjectName"));
                	projectInfo.setSize(rs.getString("Size"));
                } else {
                	return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
		return projectInfo;
	}
	public ProjectInfo get(int projectID) {
	    ProjectInfo projectInfo = null;
        java.sql.Connection cn = null;
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        String query = null;
        try {
            query = "SELECT p.* " +
                    "FROM `project` p " +
                    "WHERE p.ProjectID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, projectID);
            rs = ps.executeQuery();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                    projectInfo = new ProjectInfo();
                    projectInfo.setDepartID(rs.getInt("DepartID"));
                    projectInfo.setDescription(rs.getString("Description"));
                    projectInfo.setIsDeleted(rs.getByte("IsDeleted"));
                    projectInfo.setLastModified(rs.getString("LastModified"));
                    projectInfo.setModifiedID(rs.getString("ModifiedID"));
                    projectInfo.setPeriodFrom(rs.getString("PeriodFrom"));
                    projectInfo.setPeriodTo(rs.getString("PeriodTo"));
                    projectInfo.setProjectID(rs.getInt("ProjectID"));
                    projectInfo.setProjectName(rs.getString("ProjectName"));
                    projectInfo.setSize(rs.getString("Size"));
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        return projectInfo;
	}
	
}
